/*!
  @file nodelet.cpp
  @copyright 2013 Kubota Lab. All rights resereved.
*/

#include <ros/ros.h>
#include <nodelet/nodelet.h>
#include <boost/thread.hpp>
#include "axis_driver/camera_driver.h"

namespace axis_netcam
{

/*! 
 *  @brief  A nodelet implementation for axis driver
 *  @author Kyohei Otsu <kyon@ac.jaxa.jp>
 *  @date   2013
 *  
 *  Enables zero-copy in image transportation
 */

class CameraDriverNodelet : public nodelet::Nodelet
{
 public:
  virtual void onInit();

  void pollDevice();


 private:
  boost::shared_ptr<CameraDriver> driver_;
  boost::shared_ptr<boost::thread> dev_thread_;
  double frame_rate_;
};

void CameraDriverNodelet::onInit()
{
  driver_.reset(new CameraDriver(getNodeHandle(), 
                                 getPrivateNodeHandle(), 
                                 getPrivateNodeHandle()));
  driver_->init();
  dev_thread_.reset(new boost::thread(boost::bind(&CameraDriverNodelet::pollDevice, this)));
  /* MUST RETURN SOON */
}

void CameraDriverNodelet::pollDevice()
{
  ros::Rate rate(driver_->getFrameRate());
  while(true) 
  {
    driver_->poll();
    rate.sleep();
  }
}

}  // namespace axis_netcam


// register the nodelet
#include <pluginlib/class_list_macros.h>
PLUGINLIB_EXPORT_CLASS(axis_netcam::CameraDriverNodelet, nodelet::Nodelet) 
